home *** CD-ROM | disk | FTP | other *** search
/ Aminet 21 / Aminet 21 (1997)(GTI - Schatztruhe)[!][Oct 1997].iso / Aminet / dev / c / BinaryTrees.lzh / ubi_AVLtree.h < prev    next >
Encoding:
C/C++ Source or Header  |  1997-07-26  |  15.6 KB  |  332 lines

  1. #ifndef ubi_AVLtree_H
  2. #define ubi_AVLtree_H
  3. /* ========================================================================== **
  4.  *                              ubi_AVLtree.h
  5.  *
  6.  *  Copyright (C) 1991-1997 by Christopher R. Hertel
  7.  *
  8.  *  Email: crh@ubiqx.mn.org
  9.  * -------------------------------------------------------------------------- **
  10.  *
  11.  *  This module provides an implementation of AVL height balanced binary
  12.  *  trees.  (Adelson-Velskii, Landis 1962)
  13.  *
  14.  *  This header file contains the basic AVL structure and pointer typedefs
  15.  *  as well as the prototypes needed to access the functions in the AVL
  16.  *  module ubi_AVLtree.  The .c file implements the low-level height balancing
  17.  *  routines that manage the AVL tree, plus all of the basic primops for
  18.  *  adding, searching for, and deleting nodes.
  19.  *
  20.  * -------------------------------------------------------------------------- **
  21.  *
  22.  *  This library is free software; you can redistribute it and/or
  23.  *  modify it under the terms of the GNU Library General Public
  24.  *  License as published by the Free Software Foundation; either
  25.  *  version 2 of the License, or (at your option) any later version.
  26.  *
  27.  *  This library is distributed in the hope that it will be useful,
  28.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  29.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  30.  *  Library General Public License for more details.
  31.  *
  32.  *  You should have received a copy of the GNU Library General Public
  33.  *  License along with this library; if not, write to the Free
  34.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  35.  *
  36.  * -------------------------------------------------------------------------- **
  37.  * $Log: ubi_AVLtree.h,v $
  38.  * Revision 2.4  1997/07/26 04:36:23  crh
  39.  * Andrew Leppard, aka "Grazgur", discovered that I still had my brains tied
  40.  * on backwards with respect to node deletion.  I did some more digging and
  41.  * discovered that I was not changing the balance values correctly in the
  42.  * single rotation functions.  Double rotation was working correctly because
  43.  * the formula for changing the balance values is the same for insertion or
  44.  * deletion.  Not so for single rotation.
  45.  *
  46.  * I have tested the fix by loading the tree with over 44 thousand names,
  47.  * deleting 2,629 of them (all those in which the second character is 'u')
  48.  * and then walking the tree recursively to verify that the balance factor of
  49.  * each node is correct.  Passed.
  50.  *
  51.  * Thanks Andrew!
  52.  *
  53.  * Also:
  54.  * + Changed ubi_TRUE and ubi_FALSE to ubi_trTRUE and ubi_trFALSE.
  55.  * + Rewrote the ubi_tr<func> macros because they weren't doing what I'd
  56.  *   hoped they would do (see the bottom of the header file).  They work now.
  57.  *
  58.  * Revision 2.3  1997/06/03 05:22:07  crh
  59.  * Changed TRUE and FALSE to ubi_TRUE and ubi_FALSE to avoid causing
  60.  * problems.
  61.  *
  62.  * Revision 2.2  1995/10/03 22:15:47  CRH
  63.  * Ubisized!
  64.  *
  65.  * Revision 2.1  95/03/09  23:46:44  CRH
  66.  * Added the ModuleID static string and function.  These modules are now
  67.  * self-identifying.
  68.  * 
  69.  * Revision 2.0  95/03/05  14:11:22  CRH
  70.  * This revision of ubi_AVLtree coincides with revision 2.0 of ubi_BinTree,
  71.  * and so includes all of the changes to that module.  In addition, a bug in
  72.  * the node deletion process has been fixed.
  73.  *
  74.  * After rewriting the Locate() function in ubi_BinTree, I decided that it was
  75.  * time to overhaul this module.  In the process, I discovered a bug related
  76.  * to node deletion.  To fix the bug, I wrote function Debalance().  A quick
  77.  * glance will show that it is very similar to the Rebalance() function.  In
  78.  * previous versions of this module, I tried to include the functionality of
  79.  * Debalance() within Rebalance(), with poor results.
  80.  *
  81.  * Revision 1.0  93/10/15  22:58:48  CRH
  82.  * With this revision, I have added a set of #define's that provide a single,
  83.  * standard API to all existing tree modules.  Until now, each of the three
  84.  * existing modules had a different function and typedef prefix, as follows:
  85.  *
  86.  *       Module        Prefix
  87.  *     ubi_BinTree     ubi_bt
  88.  *     ubi_AVLtree     ubi_avl
  89.  *     ubi_SplayTree   ubi_spt
  90.  *
  91.  * To further complicate matters, only those portions of the base module
  92.  * (ubi_BinTree) that were superceeded in the new module had the new names.
  93.  * For example, if you were using ubi_AVLtree, the AVL node structure was
  94.  * named "ubi_avlNode", but the root structure was still "ubi_btRoot".  Using
  95.  * SplayTree, the locate function was called "ubi_sptLocate", but the next
  96.  * and previous functions remained "ubi_btNext" and "ubi_btPrev".
  97.  *
  98.  * This was not too terrible if you were familiar with the modules and knew
  99.  * exactly which tree model you wanted to use.  If you wanted to be able to
  100.  * change modules (for speed comparisons, etc), things could get messy very
  101.  * quickly.
  102.  *
  103.  * So, I have added a set of defined names that get redefined in any of the
  104.  * descendant modules.  To use this standardized interface in your code,
  105.  * simply replace all occurances of "ubi_bt", "ubi_avl", and "ubi_spt" with
  106.  * "ubi_tr".  The "ubi_tr" names will resolve to the correct function or
  107.  * datatype names for the module that you are using.  Just remember to
  108.  * include the header for that module in your program file.  Because these
  109.  * names are handled by the preprocessor, there is no added run-time
  110.  * overhead.
  111.  *
  112.  * Note that the original names do still exist, and can be used if you wish
  113.  * to write code directly to a specific module.  This should probably only be
  114.  * done if you are planning to implement a new descendant type, such as
  115.  * red/black trees.  CRH
  116.  *
  117.  *  V0.0 - May, 1990   -  Written by Christopher R. Hertel (CRH).
  118.  *
  119.  *  ========================================================================= **
  120.  */
  121.  
  122. #include "ubi_BinTree.h"   /* Base erg binary tree support.       */
  123.  
  124. /*  ------------------------------------------------------------------------- **
  125.  *  AVL Tree Node Structure:  This structure defines the basic elements of
  126.  *       the AVL tree nodes.  In general you *SHOULD NOT PLAY WITH THESE
  127.  *       FIELDS*!  But, of course, I have to put the structure into this
  128.  *       header so that you can use the structure as a building block.
  129.  *
  130.  *  The fields are as follows:
  131.  *    Link     -  An array of pointers.  These pointers are manipulated by the
  132.  *                BT and AVL routines, and indicate the left and right child
  133.  *                nodes, plus the parent node.  By keeping track of the parent
  134.  *                pointer, we avoid the need for recursive routines or hand-
  135.  *                tooled stacks to keep track of our path back to the root.
  136.  *                The use of these pointers is subject to change without
  137.  *                notice.
  138.  *    gender   -  For tree rebalancing purposes, it is necessary that each node
  139.  *                know whether it is the left or right child of its parent, or
  140.  *                if it is the root.  This information is stored in this field.
  141.  *    balance  -  This field is also needed for AVL balancing purposes.  It
  142.  *                indicates which subtree of the current node is longer, or if
  143.  *                the subtrees are, in fact, balanced with respect to each
  144.  *                other.
  145.  *  ------------------------------------------------------------------------- **
  146.  */
  147.  
  148. typedef struct ubi_avlNodeStruct {
  149.   struct ubi_avlNodeStruct
  150.          *Link[3];      /* Normal Binary Tree Node type.                      */
  151.   char    gender;       /* The node is either the RIGHT or LEFT child of its  */
  152.                         /* parent, or is the root node.                       */
  153.   char    balance;      /* In an AVL tree, each node is the root of a subtree */
  154.                         /* that may be balanced, or be one node longer to the */
  155.                         /* right or left.  This field keeps track of the      */
  156.                         /* balance value of each node.                        */
  157.     } ubi_avlNode;  /* Typedef'd name for an avl tree node. */
  158.  
  159. typedef ubi_avlNode *ubi_avlNodePtr;    /* a Pointer to an AVL node */
  160.  
  161. /* -------------------------------------------------------------------------- **
  162.  *  Function prototypes.
  163.  * -------------------------------------------------------------------------- **
  164.  */
  165.  
  166. ubi_avlNodePtr ubi_avlInitNode( ubi_avlNodePtr NodePtr );
  167.   /* ------------------------------------------------------------------------ **
  168.    * Initialize a tree node.
  169.    *
  170.    *  Input:   NodePtr  - a pointer to a ubi_btNode structure to be
  171.    *                      initialized.
  172.    *  Output:  a pointer to the initialized ubi_avlNode structure (ie. the
  173.    *           same as the input pointer).
  174.    * ------------------------------------------------------------------------ **
  175.    */
  176.  
  177. ubi_trBool ubi_avlInsert( ubi_btRootPtr   RootPtr,
  178.                           ubi_avlNodePtr  NewNode,
  179.                           ubi_btItemPtr   ItemPtr,
  180.                           ubi_avlNodePtr *OldNode );
  181.   /* ------------------------------------------------------------------------ **
  182.    * This function uses a non-recursive algorithm to add a new element to
  183.    * the tree.
  184.    *
  185.    *  Input:   RootPtr  -  a pointer to the ubi_btRoot structure that indicates
  186.    *                       the root of the tree to which NewNode is to be added.
  187.    *           NewNode  -  a pointer to an ubi_avlNode structure that is NOT
  188.    *                       part of any tree.
  189.    *           ItemPtr  -  A pointer to the sort key that is stored within
  190.    *                       *NewNode.  ItemPtr MUST point to information stored
  191.    *                       in *NewNode or an EXACT DUPLICATE.  The key data
  192.    *                       indicated by ItemPtr is used to place the new node
  193.    *                       into the tree.
  194.    *           OldNode  -  a pointer to an ubi_btNodePtr.  When searching
  195.    *                       the tree, a duplicate node may be found.  If
  196.    *                       duplicates are allowed, then the new node will
  197.    *                       be simply placed into the tree.  If duplicates
  198.    *                       are not allowed, however, then one of two things
  199.    *                       may happen.
  200.    *                       1) if overwritting *is not* allowed, this
  201.    *                          function will return FALSE (indicating that
  202.    *                          the new node could not be inserted), and
  203.    *                          *OldNode will point to the duplicate that is
  204.    *                          still in the tree.
  205.    *                       2) if overwritting *is* allowed, then this
  206.    *                          function will swap **OldNode for *NewNode.
  207.    *                          In this case, *OldNode will point to the node
  208.    *                          that was removed (thus allowing you to free
  209.    *                          the node).
  210.    *                          **  If you are using overwrite mode, ALWAYS  **
  211.    *                          ** check the return value of this parameter! **
  212.    *                 Note: You may pass NULL in this parameter, the
  213.    *                       function knows how to cope.  If you do this,
  214.    *                       however, there will be no way to return a
  215.    *                       pointer to an old (ie. replaced) node (which is
  216.    *                       a problem if you are using overwrite mode).
  217.    *
  218.    *  Output:  a boolean value indicating success or failure.  The function
  219.    *           will return FALSE if the node could not be added to the tree.
  220.    *           Such failure will only occur if duplicates are not allowed,
  221.    *           nodes cannot be overwritten, AND a duplicate key was found
  222.    *           within the tree.
  223.    * ------------------------------------------------------------------------ **
  224.    */
  225.  
  226. ubi_avlNodePtr ubi_avlRemove( ubi_btRootPtr  RootPtr,
  227.                               ubi_avlNodePtr DeadNode );
  228.   /* ------------------------------------------------------------------------ **
  229.    * This function removes the indicated node from the tree, after which the
  230.    * tree is rebalanced.
  231.    *
  232.    *  Input:  RootPtr  -  A pointer to the header of the tree that contains
  233.    *                      the node to be removed.
  234.    *          DeadNode -  A pointer to the node that will be removed.
  235.    *
  236.    *  Output: This function returns a pointer to the node that was removed
  237.    *          from the tree (ie. the same as DeadNode).
  238.    *
  239.    *  Note:   The node MUST be in the tree indicated by RootPtr.  If not,
  240.    *          strange and evil things will happen to your trees.
  241.    * ------------------------------------------------------------------------ **
  242.    */
  243.  
  244. int ubi_avlModuleID( int size, char *list[] );
  245.   /* ------------------------------------------------------------------------ **
  246.    * Returns a set of strings that identify the module.
  247.    *
  248.    *  Input:  size  - The number of elements in the array <list>.
  249.    *          list  - An array of pointers of type (char *).  This array
  250.    *                  should, initially, be empty.  This function will fill
  251.    *                  in the array with pointers to strings.
  252.    *  Output: The number of elements of <list> that were used.  If this value
  253.    *          is less than <size>, the values of the remaining elements are
  254.    *          not guaranteed.
  255.    *
  256.    *  Notes:  Please keep in mind that the pointers returned indicate strings
  257.    *          stored in static memory.  Don't free() them, don't write over
  258.    *          them, etc.  Just read them.
  259.    * ------------------------------------------------------------------------ **
  260.    */
  261.  
  262. /* -------------------------------------------------------------------------- **
  263.  * Masquarade...
  264.  *
  265.  * This set of defines allows you to write programs that will use any of the
  266.  * implemented binary tree modules (currently BinTree, AVLtree, and SplayTree).
  267.  * Instead of using ubi_avl... or ubi_bt, use ubi_tr... and select the tree
  268.  * type by including the appropriate module header.
  269.  */
  270.  
  271. #undef ubi_trNode
  272. #undef ubi_trNodePtr
  273. #define ubi_trNode    ubi_avlNode
  274. #define ubi_trNodePtr ubi_avlNodePtr
  275.  
  276. #undef ubi_trInitNode
  277. #define ubi_trInitNode( Np ) ubi_avlInitNode( (ubi_avlNodePtr)(Np) )
  278.  
  279. #undef ubi_trInsert
  280. #define ubi_trInsert( Rp, Nn, Ip, On ) \
  281.         ubi_avlInsert( (ubi_btRootPtr)(Rp), (ubi_avlNodePtr)(Nn), \
  282.                        (ubi_btItemPtr)(Ip), (ubi_avlNodePtr *)(On) )
  283.  
  284. #undef ubi_trRemove
  285. #define ubi_trRemove( Rp, Dn ) \
  286.         ubi_avlRemove( (ubi_btRootPtr)(Rp), (ubi_avlNodePtr)(Dn) )
  287.  
  288. #undef ubi_trLocate
  289. #define ubi_trLocate( Rp, Ip, Op ) \
  290.         (ubi_avlNodePtr)ubi_btLocate( (ubi_btRootPtr)(Rp), \
  291.                                       (ubi_btItemPtr)(Ip), \
  292.                                       (ubi_trCompOps)(Op) )
  293.  
  294. #undef ubi_trFind
  295. #define ubi_trFind( Rp, Ip ) \
  296.         (ubi_avlNodePtr)ubi_btFind( (ubi_btRootPtr)(Rp), (ubi_btItemPtr)(Ip) )
  297.  
  298. #undef ubi_trNext
  299. #define ubi_trNext( P ) (ubi_avlNodePtr)ubi_btNext( (ubi_btNodePtr)(P) )
  300.  
  301. #undef ubi_trPrev
  302. #define ubi_trPrev( P ) (ubi_avlNodePtr)ubi_btPrev( (ubi_btNodePtr)(P) )
  303.  
  304. #undef ubi_trFirst
  305. #define ubi_trFirst( P ) (ubi_avlNodePtr)ubi_btFirst( (ubi_btNodePtr)(P) )
  306.  
  307. #undef ubi_trLast
  308. #define ubi_trLast( P ) (ubi_avlNodePtr)ubi_btLast( (ubi_btNodePtr)(P) )
  309.  
  310. #undef ubi_trFirstOf
  311. #define ubi_trFirstOf( Rp, Ip, P ) \
  312.         (ubi_avlNodePtr)ubi_btFirstOf( (ubi_btRootPtr)(Rp), \
  313.                        (ubi_btItemPtr)(Ip), \
  314.                        (ubi_btNodePtr)(P) )
  315.  
  316. #undef ubi_trLastOf
  317. #define ubi_trLastOf( Rp, Ip, P ) \
  318.         (ubi_avlNodePtr)ubi_btLastOf( (ubi_btRootPtr)(Rp), \
  319.                                       (ubi_btItemPtr)(Ip), \
  320.                                       (ubi_btNodePtr)(P) )
  321.  
  322. #undef ubi_trLeafNode
  323. #define ubi_trLeafNode( Nd ) \
  324.         (ubi_avlNodePtr)ubi_btLeafNode( (ubi_btNodePtr)(Nd) )
  325.  
  326. #undef ubi_trModuleID
  327. #define ubi_trModuleID( s, l ) ubi_avlModuleID( s, l )
  328.  
  329.  
  330. /* =========================== End  ubi_AVLtree.h =========================== */
  331. #endif /* ubi_AVLtree_H */
  332.